package com.tongren.framework.core;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.tongren.framework.R;
import com.tongren.framework.widgets.banner.BannerView;
import com.tongren.framework.widgets.pull.BaseViewHolder;
import com.tongren.framework.widgets.pull.section.SectionData;

import java.util.ArrayList;

/**
 * Powered by
 */
public abstract class BaseHeaderListFragment<T> extends BaseListFragment<SectionData<T>> {
    protected static final int VIEW_TYPE_SECTION_HEADER = 1;
    protected static final int VIEW_TYPE_SECTION_CONTENT = 2;

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_SECTION_HEADER) {
            return onCreateSectionHeaderViewHolder(parent);
        }
        return onCreateSectionViewHolder(parent, viewType);
    }

    protected abstract BaseViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);

    private BaseViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.list_header, parent, false);
        return new SectionHeaderViewHolder(view);
    }

    @Override
    protected int getItemType(int position) {
        return mDataList.get(position).isHeader ? VIEW_TYPE_SECTION_HEADER : VIEW_TYPE_SECTION_CONTENT;
    }

    @Override
    protected boolean isSectionHeader(int position) {
        return mDataList.get(position).isHeader;
    }

    private class SectionHeaderViewHolder extends BaseViewHolder implements BannerView.OnBannerItemClickListener{
        private final BannerView mBannerView;

        public SectionHeaderViewHolder(View view) {
            super(view);
            mBannerView = (BannerView) view.findViewById(R.id.mBannerView);
//            header = (TextView) view.findViewById(R.id.header);
        }

        @Override
        public void onBindViewHolder(int position) {
//            header.setText(mDataList.get(position).header);
            ArrayList<BannerView.Banner> banners = new ArrayList<>();
            banners.add(new BannerView.Banner(R.drawable.banner_anquan));
            banners.add(new BannerView.Banner(R.drawable.banner_fengcai));
            banners.add(new BannerView.Banner(R.drawable.banner_xiaoyuan));
            mBannerView.setUpData(banners, this);
        }

        @Override
        public void onItemClick(View view, int position) {
        }

        @Override
        public void onBannerClick(int index, BannerView.Banner banner) {
            Toast.makeText(getActivity(), "点击了header"+index, Toast.LENGTH_SHORT).show();
        }
    }

}
